<template>
  <el-dialog
    v-el-drag-dialog
    :title="managerDialogTitle + suffix"
    :visible.sync="managerDialogVisible"
    width="40%"
    :before-close="handleClose"
  >
    <div>
      <el-table
        :data="managers"
        style="width: 100%"
      >
        <el-table-column label="账号" align="center">
          <template slot-scope="{row}">
            <span>{{ row.account }}</span>
          </template>
        </el-table-column>
        <el-table-column label="账号名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.userName | handleEmpty }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系方式" align="center">
          <template slot-scope="{row}">
            <span>{{ row.phone | handleEmpty }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{row}">
            <el-button type="danger" size="mini" @click="delManager(row.id)">解绑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        width="30%"
        title="绑定基地管理员"
        :visible.sync="addManagerDialog"
        append-to-body
      >
        <el-form
          ref="dataForm"
          :rules="rules"
          :model="temp"
          label-position="left"
          label-width="100px"
          style="width:100%;"
        >
          <el-form-item label="用户账号" prop="account">
            <el-select
              v-model="temp.account"
              style="width:100%;"
              filterable
              remote
              placeholder="搜索基地权限账号"
              :loading="loading"
              :filter-method="filterOption"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <span style="float: left">{{ item.label }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
              </el-option>
            </el-select>
            <!-- <el-input v-model="temp.account" /> -->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addBaseManager">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div slot="footer" class="dialog-footer">
      <!-- <el-button @click="handleClose">取 消</el-button> -->
      <el-button type="primary" @click="openAddManager">绑定基地管理员</el-button>
    </div>
  </el-dialog>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog'
import { delBaseManager, addBaseManager } from '@/api/base'
import { queryAccounts } from '@/api/account'

export default {
  name: 'ManagerDialog',
  directives: { elDragDialog },
  filters: {
    handleEmpty(value) {
      return value === '' ? '暂未录入！' : value
    }
  },
  props: {
    site: {
      type: Object,
      required: true
    },
    managers: {
      type: Array,
      required: true
    },
    managerDialogVisible: {
      type: Boolean,
      required: true
    },
    managerDialogTitle: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      suffix: '-基地管理员',
      temp: {
        account: ''
      },
      rules: {
        account: [
          {
            type: 'string',
            required: true,
            message: '请填写用户账号',
            trigger: 'change'
          }
        ]
      },
      addManagerDialog: false,
      accountQueryParams: {
        pageNumber: -1,
        limit: -1,
        role: 1
      },
      loading: false,
      options: []
    }
  },
  async created() {
    const res = await queryAccounts(this.accountQueryParams)
    this.options = res.accounts.map(item => {
      return {
        label: item.userName,
        value: item.account
      }
    })
    this.options_copy = this.options
  },
  methods: {
    delManager(id) {
      this.$confirm('是否解除当前绑定关系?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBaseManager({ id })
          .then(response => {
            this.$emit('unbond', id)
            this.$message({
              type: 'success',
              message: '解绑成功！'
            })
          })
          .catch(error => {
            console.log(error)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    addBaseManager() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          addBaseManager({
            account: this.temp.account,
            siteId: this.site.siteId,
            entId: this.site.entId
          })
            .then(response => {
              const type = response.type
              if (type === 1) {
                this.$message({
                  type: 'warning',
                  message: '账号不存在！'
                })
              } else if (type === 3) {
                this.$message({
                  type: 'warning',
                  message: '账号非基地权限账号！'
                })
              } else if (type === 2) {
                this.$message({
                  type: 'warning',
                  message: '该账号已是当前基地管理员！'
                })
              } else {
                this.$message({
                  type: 'success',
                  message: '绑定成功！'
                })
                this.addManagerDialog = false
                this.$emit('update-manager')
              }
            })
            .catch(error => {
              console.log(error)
            })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    openAddManager() {
      this.addManagerDialog = true
      this.temp = { account: '' }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    filterOption(queryString) {
      this.loading = true
      console.log(queryString)
      if (queryString === '') {
        this.options = this.options_copy
      } else {
        this.options = this.options_copy.filter(item => {
          return item.label.indexOf(queryString) !== -1 || item.value.indexOf(queryString) !== -1
        })
      }
      console.log(this.options)
      this.loading = false
    }
  }
}
</script>
